home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 138_01 / rktest1.c < prev    next >
Text File  |  1985-03-09  |  2KB  |  115 lines

  1. /*
  2.     program:    rktest1.c
  3.     created:    03-Nov-83
  4.     by:        A. Skjellum
  5.  
  6.     Copyright 1983, 1984 (c) California Institute of Technology.
  7.     All rights Reserved.  This program may be freely distributed
  8.     for all non-commercial purposes but may not be sold.
  9.  
  10.     updated:    16-Nov-83
  11.     purpose:    illustrate the use of rk4 program
  12.  
  13.     uses:        rk4.c
  14.  
  15.     summary:
  16.         integrates the differential equation:
  17.  
  18.         y'(t) + y(t) = t + 1
  19.         y(0) = 5.0.
  20.  
  21.         for which the exact solution:
  22.  
  23.         y(t) = t + 5exp(-t) is known.
  24.  
  25. */
  26.  
  27. /* constants */
  28.  
  29. #define    YZERO    5.0    /* initial value for y */
  30. #define    TSTART    0.0    /* starting time for integration */
  31. #define    TEND    10.0    /* ending time for integration */
  32. #define    STEPS    80    /* 40 steps in integration */
  33.  
  34.  
  35.  
  36. /* subroutines: */
  37.  
  38. /* exact(): returns exact solution value, given t */
  39.  
  40. double exact(t)
  41. double t;
  42. {
  43.     extern double exp();    /* exponential function */
  44.  
  45.     if(t)
  46.         return((t + YZERO*exp(-t)));
  47.  
  48.     return(YZERO);
  49. }
  50.  
  51. /* fn(t,y): return f(t,y) given t,y values */
  52.  
  53. double fn(t,y)
  54. double t;
  55. double y;
  56. {
  57.     /*
  58.         differential equation is y' + y = t + 1
  59.  
  60.         therefore, f = t + 1 - y.
  61.  
  62.     */
  63.  
  64.     return(t + 1.0 - y);
  65. }
  66.  
  67. /* solutn(): print solution step at console */
  68.  
  69. solutn(t,y)
  70. double *t;    /* pointer to t value */
  71. double *y;    /* pointer to y value */
  72. {
  73.     printf("t = %7.3e, y = %7.3e, y_exact = %7.3e, diff = %7.3e\n",
  74.         *t,*y,exact(*t),*y - exact(*y));
  75. }
  76.  
  77. /* main program: */
  78.  
  79. main()
  80. {
  81.     /* external declarations */
  82.  
  83.     double fn();    /* ensure that this is typed as double */
  84.  
  85.     /* local variables: */
  86.  
  87.     register int i;
  88.  
  89.     double yarray[STEPS],tarray[STEPS];
  90.         /* integrated solution stored here */
  91.  
  92.  
  93.     /* begin code: */
  94.  
  95.     printf("\n\nrktest1.c    as of 03-Nov-83\n\n");
  96.     printf("Integrates: y' + y = 1 + t     for\n\n");
  97.     printf("t = %7.3e to %7.3e, with %u steps\n\n",
  98.         TSTART,TEND,STEPS);
  99.  
  100.     /*
  101.         integrate the answer from t = 0 to t = 10 sec 
  102.         80 points.
  103.     */
  104.  
  105.     rk4(fn,TSTART,TEND,STEPS,YZERO,tarray,yarray);
  106.             /* compute the answers */
  107.  
  108.  
  109.     for(i=0;i<STEPS;i++)    /* print solution */
  110.         solutn(tarray+i,yarray+i);
  111.  
  112.  
  113.     printf("\n\nEnd of execution\n\n");
  114. }
  115.